/*
 * xAPelement.java
 *
 * Java class used to represent MyOpenLab xAP elements (endpoints)
 *
 * Copyright (c) 2009 Daniel Berenguer <dberenguer@usapiens.com>
 *
 * This file is part of the jxAP project.
 *
 * OPNODE is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * OPNODE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with canTracer; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * 07/10/09 by Daniel Berenguer : first version
 */

package xAP;


import VisualLogic.*;

import xAP.xAPtypes.TypeOfElement;


/********************************************************************************************
 * xAPelement class
 *
 * xAP element (endpoint) class
 *
 *********************************************************************************************/
public class xAPelement
{
   /********************************************************************************************
    * Member variables
    ********************************************************************************************/
    // xAP header fields
    private String source = "";      // xAP source

    // Type of xAP element
    private TypeOfElement type = TypeOfElement.NOTYPE;

    // Driver owner data
    private MyOpenLabDriverOwnerIF drvOwner = null;

   /********************************************************************************************
    * xAPelement
    *
    * Class constructor
    *
    ********************************************************************************************/
    public xAPelement()
    {
    }

   /********************************************************************************************
    * getSource
    *
    * Get endpoint source address
    *
    *  Return:
    *   Source string
    ********************************************************************************************/
    public String getSource()
    {
        return source;
    }

   /********************************************************************************************
    * setSource
    *
    * Set endpoint Source address
    *
    * 'Source'     endpoint source address
    *
    ********************************************************************************************/
    public void setSource(String Source)
    {
        source = Source;
    }

   /********************************************************************************************
    * getOwner
    *
    * Get Dirver owner id
    *
    *  Return:
    *   owner id
    ********************************************************************************************/
    public MyOpenLabDriverOwnerIF getOwner()
    {
        return drvOwner;
    }

   /********************************************************************************************
    * setOwner
    *
    * Set driver owner
    *
    * 'owner'     driver owner
    *
    ********************************************************************************************/
    public void setOwner(MyOpenLabDriverOwnerIF owner)
    {
        drvOwner = owner;
    }

   /********************************************************************************************
    * getType
    *
    * Get type of xAP element
    *
    *  Return:
    *   type of element
    ********************************************************************************************/
    public TypeOfElement getType()
    {
        return type;
    }

   /********************************************************************************************
    * setType
    *
    * Set type of element
    *
    * 'type'     type of xAP element
    *
    ********************************************************************************************/
    public void setType(TypeOfElement Type)
    {
        type = Type;
    }

   /********************************************************************************************
    * getDeviceSource
    *
    * Get device Source from endpoint Source
    *
    *  Return:
    *   device source address string
    ********************************************************************************************/
    public String getDeviceSource()
    {
        int intPos = this.source.indexOf(':');

        if (intPos == -1)
            return null;

        return this.source.substring(0, intPos);
    }

   /********************************************************************************************
    * getSubSource
    *
    * Get the source subaddress of the endpoint
    *
    *  Return:
    *   subaddress string
    ********************************************************************************************/
    public String getSubSource()
    {
        int intPos = this.source.indexOf(':');

        if (intPos == -1)
            return null;

        return this.source.substring(intPos+1);
    }

   /********************************************************************************************
    * AddToTable
    *
    * Add element to the static vector of xAP elements
    *
    ********************************************************************************************/
    public void AddToTable()
    {
        drvCore.AddElementToTable(this);
    }

   /********************************************************************************************
    * SendValue
    *
    * Send endpoint value to xAP element
    *
    * 'value'   Value to send
    ********************************************************************************************/
    public void SendValue(Object value)
    {
        drvCore.SendValue(this.getSource(), value);
    }

   /********************************************************************************************
    * isOutput
    *
    * Return true if the xAP element is an output
    *
    * 'value'   Value to send
    ********************************************************************************************/
    public boolean isOutput()
    {
        if (this.type == TypeOfElement.BSC_OUTPUT_STA || this.type == TypeOfElement.BSC_OUTPUT_NUM)
            return true;
        else
            return false;
    }
}
